<template>
  <LEchart class="w-100% h-100%" ref="chartRef" />
</template>

<script setup lang="ts">
import LEchart from './l-echart/l-echart.vue';
import { ref, watch, nextTick, onMounted } from 'vue';

import * as echarts from './static/echarts.esm.min.js';

const props = defineProps({
  options: {
    type: Object,
    default: () => ({
      tooltip: {
        trigger: 'item',
      },
      legend: {
        show: false,
        orient: 'vertical',
        left: 'left',
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: '50%',
          data: [
            { value: 1048, name: 'Search Engine' },
            { value: 735, name: 'Direct' },
            { value: 0, name: 'Email' },
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)',
            },
          },

          label: {
            normal: {
              show: true,
              position: 'inner', // 数值显示在内部
              formatter: '{d}%', // 格式化数值百分比输出
            },
          },
        },
      ],
    }),
  },
});

const chartRef = ref(null);

const initData = async (data) => {
  await nextTick();
  if (chartRef.value) {
    console.log(data, chartRef.value);
    const myChart = await chartRef.value.init(echarts);
    myChart.setOption(data);
  }
};

onMounted(() => {
  setTimeout(() => {
    initData(props.options);
  }, 200);
});

watch(
  () => props.options,
  (newValue) => {
    initData(newValue);
  },
  {
    deep: true,
  }
);
</script>

<style lang="scss" scoped></style>
